package com.huawei.livewallpaper.bnh.ts;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.service.wallpaper.WallpaperService;
import androidx.annotation.RequiresApi;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;

public class CameraLiveWallpaper extends WallpaperService {
    ScreenListener screenListener;
    MediaPlayer mMediaPlayer;
    Boolean isPlay=false;
    Boolean isSeek=false;

    // 实现WallpaperService必须实现的抽象方法  
    String TAG="shoushi";
    @Override public Engine onCreateEngine() {

        mMediaPlayer=new MediaPlayer();
        startListenerScreen();
        return new CameraEngine();
    }


//

    class CameraEngine extends Engine  {
        private Camera camera;
        float mPosX;
        float mPosY;
        float mCurPosX;
        float mCurPosY;

        String TAG="shoushi";

        int nowPage=1;
       int flag=0;//上一次的方向 1左  2右
        boolean lastIsadd=false;

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {

            super.onCreate(surfaceHolder);

//            startPreview();
            // 设置处理触摸事件
//            setTouchEventsEnabled(true);

        }


        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onSurfaceCreated(final SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
            Log.d(TAG,"onSurfaceCreated-------getAppTaskThumbnailSize"+  mActivityManager.getAppTaskThumbnailSize());
            final Surface surface = holder.getSurface();

            mMediaPlayer.setSurface(holder.getSurface());
            isSeek=true;
            play("android.resource://"+getPackageName()+"/"+ R.raw.va);



            Log.d(TAG,"onSurfaceCreated");
                mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        Log.d(TAG,"播放完成一直播放视频2");
                        Canvas canvas = null;
//                        try {
////                            InputStream inputStream = assets.open("bz_${hour}.jpg") ;//filename是assets目录下的图片名
//                            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bg);
//
//                            Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
//
//                             canvas = holder.lockCanvas();
//                            canvas.drawBitmap(mBitmap, 0f, 0f, new Paint());
//                            bitmap.recycle();
//
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        } finally {
//                            holder.unlockCanvasAndPost(canvas);
//                        }
                        isSeek=true;
                        play("android.resource://"+getPackageName()+"/"+ R.raw.va);

                       Log.d(TAG,"onCompletion");
                    }
                });



        }








        @Override
        public void onDestroy() {
            super.onDestroy();
            screenListener.unregisterListener();
//            stopPreview();

            Log.d(TAG,"销毁---onDestroy");
//            mMediaPlayer.release();
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            //播放视频2
//            play("android.resource://"+getPackageName()+"/"+ R.raw.video_2);
//            if (visible&&isPlay) {
//                isSeek=true;
//                play("android.resource://"+getPackageName()+"/"+ R.raw.va);
//
//                Log.d(TAG,"onVisibilityChanged--visible --true");
////
//            } else {
////                play("android.resource://"+getPackageName()+"/"+ R.raw.video_1);
//                try{
//
////                    mMediaPlayer.pause();
//                }catch (Exception e){
//
//                }
//
//            }

        }











    }
    private void startListenerScreen(){
        screenListener = new ScreenListener( getApplication()) ;
        screenListener.begin(new ScreenListener.ScreenStateListener() {
            @Override
            public void onScreenOn() {

//                    play("android.resource://"+getPackageName()+"/"+ R.raw.video_1);
                Log.d(TAG,"屏幕打开了");
//                Toast.makeText( MainActivity.this , "屏幕打开了" , Toast.LENGTH_SHORT ).show();
            }

            @Override
            public void onScreenOff() {

//                play("android.resource://"+getPackageName()+"/"+ R.raw.video_1);
                mMediaPlayer.pause();
                isPlay=false;
                Log.d(TAG,"屏幕关闭了");
                play("android.resource://"+getPackageName()+"/"+ R.raw.va);
                mMediaPlayer.pause();

//                Toast.makeText( this , "屏幕关闭了" , Toast.LENGTH_SHORT ).show();
            }

            @Override
            public void onUserPresent() {
                isPlay=true;
                isSeek=false;
                play("android.resource://"+getPackageName()+"/"+ R.raw.va);
                Log.d(TAG,"解锁了播放视频1");

//                Toast.makeText( MainActivity.this , "解锁了" , Toast.LENGTH_SHORT ).show();
            }
        });

    }
    private void play(String uri) {


        mMediaPlayer.reset();




        try {
//            if(mMediaPlayer.isPlaying()){
//                mMediaPlayer.stop();
//                mMediaPlayer.release();
//            }
            mMediaPlayer.setDataSource(
                    CameraLiveWallpaper.this,
                    Uri.parse(uri)

            );

            mMediaPlayer.prepare();
            if(isSeek){
                mMediaPlayer.seekTo(5600);
            }

            mMediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}  